package cn.cdiothree.service.impl;


import cn.cdiothree.dao.ProductCollectMapper;
import cn.cdiothree.dao.ProductInfoMapper;
import cn.cdiothree.dataobject.ProductCollect;
import cn.cdiothree.dataobject.ProductInfo;
import cn.cdiothree.enums.CollectEnum;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.service.ProductCollectService;
import cn.cdiothree.util.KeyUtil;
import cn.cdiothree.vo.ProductCollectVo;
import cn.cdiothree.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
/**
 * 商品收藏服务实现类
 * @author cumthing
 */
public class ProductCollectServiceImpl extends ServiceImpl<ProductCollectMapper, ProductCollect> implements ProductCollectService {
    @Autowired
    private ProductCollectMapper productCollectMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;

    /**
     * 添加或取消收藏
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public ResultVo addOrCancelCollect(Integer productId, Integer userId){
        ProductCollect aProductCollect = selectByProductIdAndUserId(productId,userId);
        //如果没有收藏过
        if (aProductCollect == null) {
            ProductCollect productCollect = new ProductCollect();
            productCollect.setCollectId(KeyUtil.genUniqueKey());
            productCollect.setUserId(userId);
            productCollect.setProductId(productId);
            productCollect.setStatus(CollectEnum.OWN.getCode());
            int insert = productCollectMapper.insert(productCollect);
            if (insert<1){
                throw new SellException(ResultEnum.COLLECT_FAILED);
            }
            log.info("【收藏】用户 {}收藏 {}",userId,productId);
            return ResultVo.success("收藏成功啦！");
        }
        //如果收藏过就是取消收藏
        else if (aProductCollect.getStatus()==1){
            aProductCollect.setStatus(CollectEnum.LOSE.getCode());
            aProductCollect.setUpdateTime(LocalDateTime.now());
            int i =productCollectMapper.updateById(aProductCollect);
            if (i==1){
                log.info("【取消收藏】用户{}取消收藏{}",userId,productId);
                return ResultVo.success("取消收藏成功啦！");
            }else {
                throw new SellException(ResultEnum.COLLECT_LOSE_FAILED);
            }
        }else if (aProductCollect.getStatus()!=1){
            aProductCollect.setStatus(CollectEnum.OWN.getCode());
            aProductCollect.setUpdateTime(LocalDateTime.now());
            int i = productCollectMapper.updateById(aProductCollect);
            if (i==1){
                log.info("【再次收藏】用户{}再次收藏 {}",userId,productId);
                return ResultVo.success("收藏成功啦！");
            }else {
                throw new SellException(ResultEnum.COLLECT_FAILED);
            }
        }
        throw new SellException(ResultEnum.COLLECT_FAILED);
    }


    /**
     * 根据商品id和用户id查询收藏情况
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public ProductCollect selectByProductIdAndUserId(Integer productId,Integer userId){
        log.info("用户{}查询商品{}收藏情况",userId,productId);
        QueryWrapper<ProductCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId).eq("user_id",userId);
        return productCollectMapper.selectOne(queryWrapper);
    }

    /**
     * 查看用户收藏列表
     * @param userId
     * @return
     */
    @Override
    public List<ProductCollectVo> list(Integer userId){
        log.info("用户{}查看商品收藏列表",userId);
        QueryWrapper<ProductCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ProductCollect> productCollectList = productCollectMapper.selectList(wrapper);
        List<ProductCollect> productCollectList1 = new ArrayList<>();
        for (ProductCollect productCollect:productCollectList){
            if (productCollect.getStatus()==1){
                productCollectList1.add(productCollect);
            }
        }
        List<ProductCollectVo> productCollectVoList = new ArrayList<>();
        ProductInfo productInfo = new ProductInfo();
        for (ProductCollect productCollect:productCollectList1){
            productInfo=productInfoMapper.selectById(productCollect.getProductId());
            ProductCollectVo productCollectVO = new ProductCollectVo();
            BeanUtils.copyProperties(productCollect,productCollectVO);
            productCollectVO.setProductInfo(productInfo);
            productCollectVoList.add(productCollectVO);
        }
        return productCollectVoList;
    }
}
